//公共二级弹层
(function () {
    var io = ppt.io;
    function Mail(data) {
        Mail.super(this);
        this.width = 1334;
        this.height = Laya.stage.height;
        this._mailbox = null;
        this._mask = null;
        this._data = data;

        io.register("mail::readMail", this, this.readMail);
        io.register("mail::readAllMail", this, this.readAllMail);
        this.init(data);
    }
    //继承
    Laya.class(Mail, "Alert.Mail", Laya.Sprite);

    var _proto = Mail.prototype;
    _proto.destroy = function () {
        Laya.Sprite.prototype.destroy.call(this, true);
        this._mailbox = null;
        this._mask = null;
    }
    //初始化
    _proto.init = function (data) {
        var mailbox = new ui.Alert.MailUI();
        mailbox.mailList.array = [];
        mailbox.x = Laya.stage.width / 2 - mailbox.width / 2;
        mailbox.y = Laya.stage.height / 2 - mailbox.height / 2;
        mailbox.zOrder = 4;
        this.addChild(mailbox);
        this._mask = new Laya.Label();
        this._mask.width = Laya.stage.width;
        this._mask.height = Laya.stage.height;
        this._mask.bgColor = '#000';
        this._mask.alpha = 0.3;
        this._mask.zOrder = 3;
        this._mask.mouseEnabled = true;
        this.addChild(this._mask);
        if (data.length == 0) {
            mailbox.mailList.visible = false;
            mailbox.noMessage.visible = true;
            mailbox.btnHave.visible = false;
        } else {
            mailbox.mailList.visible = true;
            mailbox.noMessage.visible = false;
            mailbox.btnHave.visible = true;
            for (var i = 0; i < data.length; i++) {
                data[i].mailcontent = data[i].title;
                mailbox.mailList.array = data;
            }
        }

        mailbox.mailList.mouseHandler = new laya.utils.Handler(this, this.onSelect);
        mailbox.close.on(Laya.Event.CLICK, this, this.Close);
        mailbox.btnHave.on(Laya.Event.CLICK, this, this.mailAll);
    }
    _proto.mailAll = function () {
        io.emit({ "cmd": "mail::readAllMail", params: {} });
    }
    //选择邮件
    _proto.onSelect = function (e, index) {
        if (e.type == Laya.Event.CLICK) {
            this.destroy(true);

            io.emit({ "cmd": "mail::readMail", params: { 'mailId': this._data[index].id } });

            var mailDetail = new Alert.MailDetail(this._data, index);
            Laya.stage.addChild(mailDetail);
        }
    }
    //处理字符过长
    _proto.strCount = function (text, long) {
        var text = text + '';
        var reallong = text.length;             //字节长度
        var strLength = text.length;          //字符串长度
        var maxlong = long;
        var textold = '';
        var textoldLength;
        var textnew = '';
        if (reallong <= maxlong) {
            return text;
        } else {
            for (var i = 0; i < strLength; i++) {
                textold += text[i];
                textoldLength = textold.length;
                if (textoldLength <= maxlong) {
                    textnew = textold;
                } else {
                    return textnew + '...';
                }
            }
        }
    }
    _proto.readMail = function () {
        io.emit({ 'cmd': 'game::enter', params: {} });
        io.emit({ "cmd": "mail::getMail" });
    }
    _proto.readAllMail = function (data) {
        io.emit({ 'cmd': 'game::enter', params: {} });
        io.emit({ "cmd": "mail::getMail" });
        //var UI = new Alert.PublicCt(1,data.msg);
        //ppt.director.popScene(UI);
        var config = {
            msg: data.msg,
            close: true,
            btn: {
                btnCancel: {
                    skin: "cancel"
                },
                btnConfirm: {
                    skin: "confirm"
                }
            }
        }
        ppt.director.popScene(new Alert.Public(config));
    }
    _proto.Close = function () {
        io.unregister("mail::readMail", this.readMail);
        io.unregister("mail::readAllMail", this.readAllMail);
        this.destroy(true);
    }
    _proto.onload = function () {
        window.location.reload();
    }
})();
